package com.weng.platform.article.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.article.entity.ArticleContent;
import com.weng.platform.article.model.vo.ArticleContentVo;
import com.weng.platform.article.service.IArticleContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 文章内容表 前端控制器
 *
 * @author wenghui
 * @date 2019-11-29 14:48:05
 *
 */  
@Slf4j
@RestController
@RequestMapping("/content")
@Api(value = "文章内容表api", tags = {"文章内容表操作接口"})
public class ArticleContentApi extends BaseController {
    @Autowired
    private IArticleContentService contentService;

    /**
     * 功能描述:单个获取文章内容表
     */
    @ApiOperation(value = "获取文章内容表", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        ArticleContent content = contentService.getById(id);
        return success(content);
    }


    /**
     * 条件 分页查询文章内容表
     */
    @ApiOperation(value = "分页获取文章内容表", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<ArticleContent> iPage = contentService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增文章内容表
	 */
	@ApiOperation(value = "新增文章内容表", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody ArticleContentVo contentReq){
		ArticleContent content = Convert.convert(ArticleContent.class, contentReq);
		contentService.add(content);
		return success();
	}

	/**
	 * 更新文章内容表
	 */
	@ApiOperation(value = "更新文章内容表", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody ArticleContentVo contentReq){
		ArticleContent content = Convert.convert(ArticleContent.class, contentReq);
		int flag = contentService.update(content);
		return success(flag);
	}


	/**
	 * 删除文章内容表
	 */
	@ApiOperation(value = "ids 删除文章内容表", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		boolean flag = contentService.removeByIds(Arrays.asList(ids.split(",")));
		if(flag){
			return success(deleteReq.getIds());
		}else{
			return failed("删除失败");
		}
	}
}
